<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="发货日期" prop="deliveryDate">
      <el-input v-model="dataForm.deliveryDate" placeholder="发货日期"></el-input>
    </el-form-item>
    <el-form-item label="业务员" prop="salesperson">
      <el-input v-model="dataForm.salesperson" placeholder="业务员"></el-input>
    </el-form-item>
    <el-form-item label="客户名称" prop="customerName">
      <el-input v-model="dataForm.customerName" placeholder="客户名称"></el-input>
    </el-form-item>
    <el-form-item label="销售发货/退货" prop="salesDeliveryReturn">
      <el-input v-model="dataForm.salesDeliveryReturn" placeholder="销售发货/退货"></el-input>
    </el-form-item>
    <el-form-item label="编号" prop="number">
      <el-input v-model="dataForm.number" placeholder="编号"></el-input>
    </el-form-item>
    <el-form-item label="产品名称" prop="productName">
      <el-input v-model="dataForm.productName" placeholder="产品名称"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="quantity">
      <el-input v-model="dataForm.quantity" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="单价" prop="unitPrice">
      <el-input v-model="dataForm.unitPrice" placeholder="单价"></el-input>
    </el-form-item>
    <el-form-item label="金额（开票）" prop="invoiceAmount">
      <el-input v-model="dataForm.invoiceAmount" placeholder="金额（开票）"></el-input>
    </el-form-item>
    <el-form-item label="所属区域" prop="region">
      <el-input v-model="dataForm.region" placeholder="所属区域"></el-input>
    </el-form-item>
    <el-form-item label="开票公司" prop="invoiceCompany">
      <el-input v-model="dataForm.invoiceCompany" placeholder="开票公司"></el-input>
    </el-form-item>
    <el-form-item label="结算价" prop="settlementPrice">
      <el-input v-model="dataForm.settlementPrice" placeholder="结算价"></el-input>
    </el-form-item>
    <el-form-item label="指导价" prop="guidePrice">
      <el-input v-model="dataForm.guidePrice" placeholder="指导价"></el-input>
    </el-form-item>
    <el-form-item label="到期时间" prop="expireDate">
      <el-input v-model="dataForm.expireDate" placeholder="到期时间"></el-input>
    </el-form-item>
    <el-form-item label="超期天数" prop="overdueDays">
      <el-input v-model="dataForm.overdueDays" placeholder="超期天数"></el-input>
    </el-form-item>
    <el-form-item label="回款银行" prop="paymentBank">
      <el-input v-model="dataForm.paymentBank" placeholder="回款银行"></el-input>
    </el-form-item>
    <el-form-item label="回款日期" prop="paymentDate">
      <el-input v-model="dataForm.paymentDate" placeholder="回款日期"></el-input>
    </el-form-item>
    <el-form-item label="对公回款金额" prop="publicPaymentAmount">
      <el-input v-model="dataForm.publicPaymentAmount" placeholder="对公回款金额"></el-input>
    </el-form-item>
    <el-form-item label="单位应收款" prop="unitReceivable">
      <el-input v-model="dataForm.unitReceivable" placeholder="单位应收款"></el-input>
    </el-form-item>
    <el-form-item label="回款日期（个人其他）" prop="personalPaymentDate">
      <el-input v-model="dataForm.personalPaymentDate" placeholder="回款日期（个人其他）"></el-input>
    </el-form-item>
    <el-form-item label="单位应收（个人）" prop="personalUnitReceivable">
      <el-input v-model="dataForm.personalUnitReceivable" placeholder="单位应收（个人）"></el-input>
    </el-form-item>
    <el-form-item label="回款日期（业务员）" prop="salespersonPaymentDate">
      <el-input v-model="dataForm.salespersonPaymentDate" placeholder="回款日期（业务员）"></el-input>
    </el-form-item>
    <el-form-item label="单位应收(业务员）" prop="salespersonUnitReceivable">
      <el-input v-model="dataForm.salespersonUnitReceivable" placeholder="单位应收(业务员）"></el-input>
    </el-form-item>
    <el-form-item label="汇率" prop="exchangeRate">
      <el-input v-model="dataForm.exchangeRate" placeholder="汇率"></el-input>
    </el-form-item>
    <el-form-item label="美元" prop="usd">
      <el-input v-model="dataForm.usd" placeholder="美元"></el-input>
    </el-form-item>
    <el-form-item label="汇兑" prop="exchangeRateConversion">
      <el-input v-model="dataForm.exchangeRateConversion" placeholder="汇兑"></el-input>
    </el-form-item>
    <el-form-item label="录入人" prop="createdBy">
      <el-input v-model="dataForm.createdBy" placeholder="录入人"></el-input>
    </el-form-item>
    <el-form-item label="录入时间" prop="createdAt">
      <el-input v-model="dataForm.createdAt" placeholder="录入时间"></el-input>
    </el-form-item>
    <el-form-item label="数据有效性" prop="isValid">
      <el-input v-model="dataForm.isValid" placeholder="数据有效性"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          deliveryDate: '',
          salesperson: '',
          customerName: '',
          salesDeliveryReturn: '',
          number: '',
          productName: '',
          quantity: '',
          unitPrice: '',
          invoiceAmount: '',
          region: '',
          invoiceCompany: '',
          settlementPrice: '',
          guidePrice: '',
          expireDate: '',
          overdueDays: '',
          paymentBank: '',
          paymentDate: '',
          publicPaymentAmount: '',
          unitReceivable: '',
          personalPaymentDate: '',
          personalUnitReceivable: '',
          salespersonPaymentDate: '',
          salespersonUnitReceivable: '',
          exchangeRate: '',
          usd: '',
          exchangeRateConversion: '',
          createdBy: '',
          createdAt: '',
          isValid: ''
        },
        dataRule: {
          deliveryDate: [
            { required: true, message: '发货日期不能为空', trigger: 'blur' }
          ],
          salesperson: [
            { required: true, message: '业务员不能为空', trigger: 'blur' }
          ],
          customerName: [
            { required: true, message: '客户名称不能为空', trigger: 'blur' }
          ],
          salesDeliveryReturn: [
            { required: true, message: '销售发货/退货不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '编号不能为空', trigger: 'blur' }
          ],
          productName: [
            { required: true, message: '产品名称不能为空', trigger: 'blur' }
          ],
          quantity: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          unitPrice: [
            { required: true, message: '单价不能为空', trigger: 'blur' }
          ],
          invoiceAmount: [
            { required: true, message: '金额（开票）不能为空', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],
          invoiceCompany: [
            { required: true, message: '开票公司不能为空', trigger: 'blur' }
          ],
          settlementPrice: [
            { required: true, message: '结算价不能为空', trigger: 'blur' }
          ],
          guidePrice: [
            { required: true, message: '指导价不能为空', trigger: 'blur' }
          ],
          expireDate: [
            { required: true, message: '到期时间不能为空', trigger: 'blur' }
          ],
          overdueDays: [
            { required: true, message: '超期天数不能为空', trigger: 'blur' }
          ],
          paymentBank: [
            { required: true, message: '回款银行不能为空', trigger: 'blur' }
          ],
          paymentDate: [
            { required: true, message: '回款日期不能为空', trigger: 'blur' }
          ],
          publicPaymentAmount: [
            { required: true, message: '对公回款金额不能为空', trigger: 'blur' }
          ],
          unitReceivable: [
            { required: true, message: '单位应收款不能为空', trigger: 'blur' }
          ],
          personalPaymentDate: [
            { required: true, message: '回款日期（个人其他）不能为空', trigger: 'blur' }
          ],
          personalUnitReceivable: [
            { required: true, message: '单位应收（个人）不能为空', trigger: 'blur' }
          ],
          salespersonPaymentDate: [
            { required: true, message: '回款日期（业务员）不能为空', trigger: 'blur' }
          ],
          salespersonUnitReceivable: [
            { required: true, message: '单位应收(业务员）不能为空', trigger: 'blur' }
          ],
          exchangeRate: [
            { required: true, message: '汇率不能为空', trigger: 'blur' }
          ],
          usd: [
            { required: true, message: '美元不能为空', trigger: 'blur' }
          ],
          exchangeRateConversion: [
            { required: true, message: '汇兑不能为空', trigger: 'blur' }
          ],
          createdBy: [
            { required: true, message: '录入人不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '录入时间不能为空', trigger: 'blur' }
          ],
          isValid: [
            { required: true, message: '数据有效性不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/cw/deliverypaymentrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deliveryDate = data.deliveryPaymentRecord.deliveryDate
                this.dataForm.salesperson = data.deliveryPaymentRecord.salesperson
                this.dataForm.customerName = data.deliveryPaymentRecord.customerName
                this.dataForm.salesDeliveryReturn = data.deliveryPaymentRecord.salesDeliveryReturn
                this.dataForm.number = data.deliveryPaymentRecord.number
                this.dataForm.productName = data.deliveryPaymentRecord.productName
                this.dataForm.quantity = data.deliveryPaymentRecord.quantity
                this.dataForm.unitPrice = data.deliveryPaymentRecord.unitPrice
                this.dataForm.invoiceAmount = data.deliveryPaymentRecord.invoiceAmount
                this.dataForm.region = data.deliveryPaymentRecord.region
                this.dataForm.invoiceCompany = data.deliveryPaymentRecord.invoiceCompany
                this.dataForm.settlementPrice = data.deliveryPaymentRecord.settlementPrice
                this.dataForm.guidePrice = data.deliveryPaymentRecord.guidePrice
                this.dataForm.expireDate = data.deliveryPaymentRecord.expireDate
                this.dataForm.overdueDays = data.deliveryPaymentRecord.overdueDays
                this.dataForm.paymentBank = data.deliveryPaymentRecord.paymentBank
                this.dataForm.paymentDate = data.deliveryPaymentRecord.paymentDate
                this.dataForm.publicPaymentAmount = data.deliveryPaymentRecord.publicPaymentAmount
                this.dataForm.unitReceivable = data.deliveryPaymentRecord.unitReceivable
                this.dataForm.personalPaymentDate = data.deliveryPaymentRecord.personalPaymentDate
                this.dataForm.personalUnitReceivable = data.deliveryPaymentRecord.personalUnitReceivable
                this.dataForm.salespersonPaymentDate = data.deliveryPaymentRecord.salespersonPaymentDate
                this.dataForm.salespersonUnitReceivable = data.deliveryPaymentRecord.salespersonUnitReceivable
                this.dataForm.exchangeRate = data.deliveryPaymentRecord.exchangeRate
                this.dataForm.usd = data.deliveryPaymentRecord.usd
                this.dataForm.exchangeRateConversion = data.deliveryPaymentRecord.exchangeRateConversion
                this.dataForm.createdBy = data.deliveryPaymentRecord.createdBy
                this.dataForm.createdAt = data.deliveryPaymentRecord.createdAt
                this.dataForm.isValid = data.deliveryPaymentRecord.isValid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/cw/deliverypaymentrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'deliveryDate': this.dataForm.deliveryDate,
                'salesperson': this.dataForm.salesperson,
                'customerName': this.dataForm.customerName,
                'salesDeliveryReturn': this.dataForm.salesDeliveryReturn,
                'number': this.dataForm.number,
                'productName': this.dataForm.productName,
                'quantity': this.dataForm.quantity,
                'unitPrice': this.dataForm.unitPrice,
                'invoiceAmount': this.dataForm.invoiceAmount,
                'region': this.dataForm.region,
                'invoiceCompany': this.dataForm.invoiceCompany,
                'settlementPrice': this.dataForm.settlementPrice,
                'guidePrice': this.dataForm.guidePrice,
                'expireDate': this.dataForm.expireDate,
                'overdueDays': this.dataForm.overdueDays,
                'paymentBank': this.dataForm.paymentBank,
                'paymentDate': this.dataForm.paymentDate,
                'publicPaymentAmount': this.dataForm.publicPaymentAmount,
                'unitReceivable': this.dataForm.unitReceivable,
                'personalPaymentDate': this.dataForm.personalPaymentDate,
                'personalUnitReceivable': this.dataForm.personalUnitReceivable,
                'salespersonPaymentDate': this.dataForm.salespersonPaymentDate,
                'salespersonUnitReceivable': this.dataForm.salespersonUnitReceivable,
                'exchangeRate': this.dataForm.exchangeRate,
                'usd': this.dataForm.usd,
                'exchangeRateConversion': this.dataForm.exchangeRateConversion,
                'createdBy': this.dataForm.createdBy,
                'createdAt': this.dataForm.createdAt,
                'isValid': this.dataForm.isValid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
